<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="isShowDialog = true"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="name"
            label="名称"
            width="240"
          />
          <el-table-column align="center" prop="description" label="描述" />
          <el-table-column align="center" label="操作" width="240">
            <template slot-scope="scope">
              <el-button size="small" type="success">分配权限</el-button>
              <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            :current-page="page.page"
            :page-size="page.pagesize"
            :total="page.total"
            layout="prev, pager, next"
            @current-change="changePage"
          />
        </el-row>
      </el-card>
    </div>
    <el-dialog
      :title="roleForm.id?'编辑角色':'新增角色'"
      :visible="isShowDialog"
      center
      @close="cancel"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addRoles">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/settings'
export default {
  name: 'Settings',
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      isShowDialog: false,
      roleList: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      }
    }
  },
  mounted() {
    this.renderRoleList() // 获取角色列表
  },
  methods: {
    async renderRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.roleList = rows
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.renderRoleList()
    },
    editRole(id) {
      console.log(id)
      // 1.展开弹框
      this.isShowDialog = true
      // 2.回填数据
      this.hGetRoleDetail(id)
    },
    // 获取角色详情
    async hGetRoleDetail(id) {
      const res = await getRoleDetail(id)
      // 回填操作
      this.roleForm = res
    },
    delRole(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.renderRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addRoles() {
      this.$refs.roleForm.validate(validate => {
        if (validate) {
          // 编辑
          if (this.roleForm.id) {
            updateRole(this.roleForm).then(res => {
              // 重新拉取数据
              this.renderRoleList()
              this.$message.success('更新角色成功')
              this.$refs.roleForm.resetFields()
              this.isShowDialog = false
            })
          } else {
            // 新增
            addRole(this.roleForm).then(res => {
              // 重新拉取数据
              this.renderRoleList()
              this.$message.success('新增角色成功')
              this.isShowDialog = false
            })
          }
        }
      })
    },
    cancel() {
      this.roleForm = {}
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.isShowDialog = false
    }
  }
}
</script>

<style>

</style>
